package edu.gdkm.web.convert;

import edu.gdkm.web.vo.ActionResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.io.File;


@ControllerAdvice
public class MyResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    private static Log log = LogFactory.getLog(MyResponseBodyAdvice.class);

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        log.debug("MyResponseBodyAdvice==>supports:" + converterType);
        log.debug("MyResponseBodyAdvice==>supports:" + returnType.getClass());
        log.debug("MyResponseBodyAdvice==>supports:"
                + MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType));
        return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
                                  ServerHttpResponse response) {
        if (body == null) {
            return body;
        }

        if (body instanceof ActionResult || body instanceof String) {
            return body;
        } else if (body instanceof File) {
            return body;
        } else {
            log.debug("MyResponseBodyAdvice==>beforeBodyWrite:" + returnType + "," + body);
            ActionResult result = new ActionResult();
            result.setErrmsg("操作成功");
            result.setErrno(0);
            result.setData(body);
            body = (Object) result;
            return body;
        }
    }
}
